﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Hyt.Core;
using Hyt.Core.Security;

namespace Hyt.Web
{
    public class HttpContextUnitOfWork : UnitOfWork
    {
        private readonly HttpContext _context;

        public HttpContextUnitOfWork(HttpContext context)
            : base()
        {
            _context = context;
        }
        public HttpContextUnitOfWork(HttpContext context, bool usetrnas)
            : base(usetrnas)
        {
            _context = context;
        }

        protected HttpContext GetContext()
        {
            return _context;
        }

    }

    public  class HttpContextUnitOfWork<T> : UnitOfWork where T : IPrincipal,new()
    {
        private readonly HttpContext _context;
        public CookiePassportManager<T> CookiePassportManager { get; private set; }
    

        public HttpContextUnitOfWork(HttpContext context):base()
        {
            _context = context;
            thisInit();
        }
        public HttpContextUnitOfWork(HttpContext context,bool usetrnas)
            : base(usetrnas)
        {
            _context = context;
            thisInit();
        }

        protected HttpContext GetContext()
        {
            return _context;
        }

        private void thisInit()
        {
            CookiePassportManager = new CookiePassportManager<T>();
            CookiePassportManager.PrincipalResolver = this.Get<IPrincipalResolver>();
            var pcp = CookiePassportManager.GetPrincipal<T>(_context);
            this.SetPrincipal(pcp);
        }

    }
}
